<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include(APPPATH.'config/database'.EXT);

class Install extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/Install
	 *	- or -  
	 * 		http://example.com/index.php/Install/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/Install/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		$allSql = file_get_contents("application/sql/ts.sql");
		foreach (explode(";", $allSql) as $sql) {
			$sql = trim($sql);
			if($sql) {
				$this->db->query($sql);
			} 
		}
		$usersSql = file_get_contents("application/sql/ion_auth.sql");
		foreach (explode(";", $usersSql) as $sql) {
			$sql = trim($sql);
			if($sql) {
				$this->db->query($sql);
			} 
		}
		
		$this->parser->parse("install.tpl");
	}

	public function __construct()
	{
		parent::__construct();
        $db = $GLOBALS['db'];
        $active = $GLOBALS['active_group'];
        $conn = mysql_connect($db[$active]['hostname'], $db[$active]['username'], $db[$active]['password']);
        if (!$conn) {
            die('Not connected : ' . mysql_error());
        }
        $db_selected = mysql_select_db($db[$active]['database'], $conn);
        if (!$db_selected) {
            mysql_query("CREATE DATABASE IF NOT EXISTS ".$db[$active]['database'], $conn);
        }
        $this->load->database();
	}
    
}

/* End of file Install.php */
/* Location: ./application/controllers/Install.php */
